﻿Friend Class Handler_ADS
    Private Sub New()
    End Sub

    Public Shared Sub UpdateADSMetaData(ByVal item As Entities.Downloads.Item, ByVal file As Entities.Downloads.File, ByVal restoreLastWriteTime As Boolean)
        Dim strPath As String = IO.Path.Combine(file.Directory, file.Name)
        Dim clsMD As New ADSMetaData

        clsMD.ItemId = file.ItemId
        clsMD.ItemUniqueId = item.UniqueId
        clsMD.ItemName = item.Name
        clsMD.ItemType = item.ItemType
        clsMD.ItemComment = item.Comment

        clsMD.FileId = file.Id
        clsMD.FileIsComplete = file.Status = Entities.Downloads.Status.Complete
        clsMD.FilePartStatuses = file.PartStatuses
        clsMD.FileSHA512FileSize = file.Size
        clsMD.FileSHA512Hash = file.SHA512Hash
        clsMD.FileSHA512LastChanged = IO.File.GetLastWriteTimeUtc(strPath)

        clsMD.NetNodeUniqueId = Framework.Net.Settings.UniqueId

        Dim strData As String = Framework.Serialization.ToXml(clsMD)

        Dim dtOrgDate As Date
        If restoreLastWriteTime Then dtOrgDate = IO.File.GetLastWriteTimeUtc(strPath)
        Try
            Framework.IO.AlternateDataStreams.ADSFile.Write(strData, strPath, "B2BFileShareMetaData")
        Catch ex As Framework.IO.AlternateDataStreams.StreamNotFoundException
            'not supported, just exit
        Finally
            If restoreLastWriteTime Then IO.File.SetLastWriteTimeUtc(strPath, dtOrgDate)
        End Try
    End Sub
    Public Shared Sub UpdateADSMetaData(ByVal item As Entities.SharedItem, ByVal file As Entities.SharedItemFile)
        Dim strPath As String = IO.Path.Combine(file.Directory, file.FileName)
        Dim clsMD As New ADSMetaData

        clsMD.ItemId = 0
        clsMD.ItemUniqueId = item.UniqueId
        clsMD.ItemName = item.Name
        clsMD.ItemType = item.ItemType
        clsMD.ItemComment = item.Comment

        clsMD.FileId = 0
        clsMD.FileIsComplete = True
        clsMD.FilePartStatuses = Nothing
        clsMD.FileSHA512FileSize = file.Size
        clsMD.FileSHA512Hash = file.SHA512Hash
        clsMD.FileSHA512LastChanged = IO.File.GetLastWriteTimeUtc(strPath)
        clsMD.SHA512HashForFirstMb = file.SHA512HashForFirstMb
        clsMD.SHA512HashForLastMb = file.SHA512HashForLastMb

        clsMD.NetNodeUniqueId = Framework.Net.Settings.UniqueId

        Dim strData As String = Framework.Serialization.ToXml(clsMD)

        Dim dtOrgDate As Date = IO.File.GetLastWriteTimeUtc(strPath)
        Try
            Framework.IO.AlternateDataStreams.ADSFile.Write(strData, strPath, "B2BFileShareMetaData")
        Catch ex As Framework.IO.AlternateDataStreams.StreamNotFoundException
        Catch ex As Framework.IO.AlternateDataStreams.StreamNotSupportedException
        Finally
            IO.File.SetLastWriteTimeUtc(strPath, dtOrgDate)
        End Try
    End Sub

    Public Shared Function GetADSMetaDataIfValid(ByVal file As System.IO.FileInfo) As ADSMetaData
        Dim clsMD As ADSMetaData = ReadADSMetaData(file.FullName)
        If clsMD IsNot Nothing AndAlso IsValid(file, clsMD) Then
            Return clsMD
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function ReadADSMetaData(ByVal filePath As String) As ADSMetaData
        Dim strData As String
        Try
            strData = Framework.IO.AlternateDataStreams.ADSFile.Read(filePath, "B2BFileShareMetaData")
        Catch ex As Framework.IO.AlternateDataStreams.StreamNotFoundException
            Return Nothing
        End Try
        If strData Is Nothing Then Return Nothing

        Dim clsMD As ADSMetaData = Framework.Serialization.FromXml(Of ADSMetaData)(strData)
        'did the metadata originate from this node?
        If clsMD.NetNodeUniqueId <> Framework.Net.Settings.UniqueId Then
            'no, so FileId and ItemId are not valid. Clear 'em
            clsMD.FileId = 0
            clsMD.ItemId = 0
        End If
        Return clsMD
    End Function

    Public Shared Function IsValid(ByVal file As System.IO.FileInfo, ByVal metaData As ADSMetaData) As Boolean
        If file.Length <> metaData.FileSHA512FileSize Then Return False
        If file.LastWriteTimeUtc <> metaData.FileSHA512LastChanged Then Return False
        Return True
    End Function

End Class
